<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function getAdd()
    {
        return view('admin/user/add');
    }


    //插入数据
    public function postInsert(Request $request)
    {
    	//所有数据
    	//dd($request->all());
    	//数据验证 自动闪存
    	$this->validate($request, [
        'username' => 'required|max:255',
        'password' => 'required',
        'email' => 'required|email',
        'tell' => 'required|max:[1\d{10}]',
    ],[
    	'username.required'=>'用户名不能为空',
    	'username.max'=>'用户名太长了',

    	'password.required'=>'用户密码不能为空',
    	'email.required'=>'邮箱不能为空',
    	'email.email'=>'邮箱格式不正确',
    	'tell.required'=>'手机号不能为空',
    	'tell.regex'=>'请填写正确的手机号',
    ]);
    //验证成功
    //提取部分数据
    	$data = $request->only(['username','password','email','tell','auth','pic']);
    	$data['token'] = str_random(50);
    	$data['password'] = Hash::make($data['password']);
 
       //添加图片
         if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('/public/uploads/',$name.'.'.$ext);
            //echo '<img src="/uploads/'.$name.'.jpg" alt="">';

            $img = self::suofang('public/uploads/'.$name.'.'.$ext,50,50);

            // dd($data['pic']);
            $data['pic'] = $name.'.'.$ext;

        }

    	//执行数据插入
    	$res = DB::table('trip_user')->insert($data);

    	//判断是否添加成功
    	if($res){
    		return redirect('/admin/user/index')->with('success','用户添加成功');
    	}else{
    		return back()->with('error','用户添加失败');
    	}
    }

    //用户列表页
    public function getIndex(Request $request)
    {
    	//查询所有用户数据
    	// $trip_user = DB::table('trip_user')->get();
    	//获取分页大写
    	$num = $request->input('num',5);
        //搜索分页
    	if($request->input('keywords')){
    		$user = DB::table('trip_user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
    	}else{
    		$user = DB::table('trip_user')->paginate($num);
    	}
    	//提取当前请求的所有参数
    	$data = $request->all();
    	// echo '用户列表页';
    	return view('admin.user.index',['user'=>$user,'request'=>$data]);
    }

    //修改表单数据
    public function getEdit($id)
    {
    	//根据用户id查询数据
    	$user = DB::table('trip_user')->where('id',$id)->first();
    	//解析模板 显示用户数据
    	return view('admin.user.edit',['user'=>$user]);
    }

    //修改
    public function postUpdate(Request $request)
    {
    	//获取id
    	$id = $request->only(['id']);

    	//详细数据
    	$data = $request->only(['username','email','tell','pic','auth']);

         //添加图片
         if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('public/uploads/',$name.'.'.$ext);
            //echo '<img src="/uploads/'.$name.'.jpg" alt="">';
           
            $img = self::suofang('public/uploads/'.$name.'.'.$ext,50,50);
            // dd($data['pic']);
            $data['pic'] = $name.'.'.$ext;

        }

    	//执行修改
    	$res = DB::table('trip_user')->where('id',$id)->update($data);


    	//判断是否修改成功
    	if($res){
    		return redirect('/admin/user/index')->with('success','用户修改成功');
    	}else{
    		return back()->with('error','用户修改失败');
    	}
    }

    public function getDelete(Request $request)
    {
    	$id = $request->input('id');
    	//执行删除
    	$res = DB::table('trip_user')->where('id',$id)->delete();
    	if($res){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }

    //缩放图片
     public static function suofang($spath, $sw = 100, $sh = 100, $pre = 's_')
    {
    //1.创建画布
    //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
    $srcinfo = getimagesize($spath);
    //根据图片类型 去创建图像资源 ...
    $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
    
    if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
        $bili = $sh/$srcinfo[1];
    }else{
        $bili = $sw/$srcinfo[0];
    }
    //舍去法取整
    $sw = floor($srcinfo[0] * $bili);
    $sh = floor($srcinfo[1] * $bili);


    $dst = imagecreatetruecolor($sw,$sh);

    //3.开始绘画
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

    //5.将图像保存为文件
    //随机文件名  //获取文件后缀名  目录
    //$ext = strrchr($spath,'.');
    //$filename = time().rand(1000,9999).$ext;
    $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
    
    imagejpeg($dst, $filename);

    //6.销毁资源
    imagedestroy($dst);
    imagedestroy($src);
    
    //返回原文件名加前缀
    return $pre.basename($spath);
}

}
